package com.mhy.crash;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupMenu;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;

import com.mhy.crash.utils.CrashModel;
import com.mhy.crash.utils.HYCrashUtils;
import com.mhy.crash.utils.HttpUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * author : ChenPeng
 * date : 2018/4/20
 * description :
 */
public class CrashActivity extends AppCompatActivity {

    public static final String CRASH_MODEL = "crash_model";

    private final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH:mm",
            Locale.getDefault());

    private CrashModel model;

    private ScrollView sScrollView;
    private ViewGroup sToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.HYCrashTheme_Dark);
        setContentView(R.layout.activity_crash);
        model = getIntent().getParcelableExtra(CRASH_MODEL);
        if (model == null) {
            return;
        }

        sScrollView = findViewById(R.id.sScrollView);
        sToolbar = findViewById(R.id.sToolbar);
//        TextView tv_packageName = findViewById(R.id.tv_packageName);
        TextView textMessage = findViewById(R.id.sTextMessage);
        TextView tv_className = findViewById(R.id.sTvClassName);
        TextView tv_methodName = findViewById(R.id.sTvMethodName);
        TextView tv_lineNumber = findViewById(R.id.sTvLineNumber);
        TextView tv_exceptionType = findViewById(R.id.sTvExceptionType);
        TextView tv_fullException = findViewById(R.id.sTvFullException);
        TextView tv_time = findViewById(R.id.sTvTime);
        TextView tv_model = findViewById(R.id.sTvModel);
        TextView tv_brand = findViewById(R.id.sTvBrand);
        TextView tv_version = findViewById(R.id.sTvVersion);
        TextView tv_more = findViewById(R.id.sTvMore);
        TextView tv_cpuAbi = findViewById(R.id.sTvCpuAbi);
        TextView tv_versionCode = findViewById(R.id.sTvVersionCode);
        TextView tv_versionName = findViewById(R.id.sTvVersionName);
//
//        tv_packageName.setText(model.getClassName());
        textMessage.setText(model.getExceptionMsg());
        tv_className.setText(model.getFileName());
        tv_methodName.setText(model.getMethodName());
        tv_lineNumber.setText(String.valueOf(model.getLineNumber()));
        tv_exceptionType.setText(model.getExceptionType());
        tv_fullException.setText(model.getFullException());
        tv_time.setText(df.format(model.getTime()));

        CrashModel.Device device = model.getDevice();
        tv_model.setText(model.getDevice().getModel());
        tv_brand.setText(model.getDevice().getBrand());
        String platform = "Android " + device.getRelease() + "-" + device.getVersion();
        tv_version.setText(platform);

        String cpuAbi = device.getCpuAbi();
        tv_cpuAbi.setText(cpuAbi);

        tv_versionCode.setText(model.getVersionCode());
        tv_versionName.setText(model.getVersionName());

        tv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMenu(v);
            }
        });
        getMetaDataFromApp();
    }

    private String ddToken;
    private String wxToken;
    private String fsToken;

    private void getMetaDataFromApp() {
        try {
            ApplicationInfo appInfo = getPackageManager()
                    .getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
            ddToken = appInfo.metaData.getString("dd_token");
            wxToken = appInfo.metaData.getString("wx_token");
            fsToken = appInfo.metaData.getString("fs_token");
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private HttpUtils.HttpCallback<String> httpCallback = new HttpUtils.HttpCallback<String>() {
        @Override
        public void onFinish(String response) {
            Log.d("http_onFinish", response);//0
            Toast.makeText(CrashActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(Exception e) {
            Toast.makeText(CrashActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
            Log.e("http_onError", e.getMessage() + "");
        }
    };

    private void showMenu(View v) {
        PopupMenu menu = new PopupMenu(CrashActivity.this, v);
        menu.inflate(R.menu.menu_more);
        if (TextUtils.isEmpty(ddToken)) {
            menu.getMenu().findItem(R.id.menu_share_dd).setVisible(false);
        }
        if (TextUtils.isEmpty(wxToken)) {
            menu.getMenu().findItem(R.id.menu_share_wx).setVisible(false);
        }
        if (TextUtils.isEmpty(fsToken)) {
            menu.getMenu().findItem(R.id.menu_share_fs).setVisible(false);
        }
        menu.show();
        menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int id = item.getItemId();
                if (id == R.id.menu_copy_text) {
                    String crashText = getShareText(model);
                    ClipboardManager cm = (ClipboardManager) getSystemService(
                            Context.CLIPBOARD_SERVICE);
                    if (cm != null) {
                        ClipData mClipData = ClipData.newPlainText("crash", crashText);
                        cm.setPrimaryClip(mClipData);
                        showToast(getString(R.string.simpleCopied));
                    }
                } else if (id == R.id.menu_share_text) {
                    String crashText = getShareText(model);
                    shareText(crashText);
                } else if (id == R.id.menu_share_image) {
                    shareImage();
                } else if (id == R.id.menu_share_dd) {
                    String crashText = getShareText(model);

                    HttpUtils.getInstance().postJson(
                            "https://oapi.dingtalk.com/robot/send?access_token=" + ddToken,
                            "{\"msgtype\": \"text\",\"text\": {\"content\": \"通知 bug:" + crashText +
                                    "\"},\"at\": {\"atMobiles\": [],\"isAtAll\": false }}",
                            httpCallback);
                } else if (id == R.id.menu_share_wx) {
                    String crashText = getShareText(model);
                    HttpUtils.getInstance().postJson(
                            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" + wxToken,
                            "{\"msgtype\": \"text\",\"text\": {\"content\": \"" + crashText +
                                    "\",\"mentioned_list\": [],\"mentioned_mobile_list\": []}}",
                            httpCallback);
                } else if (id == R.id.menu_share_fs) {
                    String crashText = getShareText(model);
                    //普通文本不能换行
                    String text = crashText.replaceAll("\\s"," ");
                    //将\n转换为///n；便可以实现换行的操作，在正则表达式中表示一个反斜杠\，应当写成\\\\
//                    String text = crashText.replaceAll("\n","").replaceAll("\\s","\\\\n");
                    HttpUtils.getInstance().postJson(
                            "https://open.feishu.cn/open-apis/bot/v2/hook/" + fsToken,
                            "{\"msg_type\":\"text\",\"content\":{\"text\":\"" + text + "\"}}",
                            httpCallback);
                }
                return true;
            }
        });
    }


    private String getShareText(CrashModel model) {
        return HYCrashUtils.getShareText(this.getApplicationContext(), model);
    }

    private void shareText(String text) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.simpleCrashInfo));
        intent.putExtra(Intent.EXTRA_TEXT, text);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent.createChooser(intent, getString(R.string.simpleShareTo)));
    }

    public Bitmap getBitmapByView(ViewGroup toolbar, ScrollView scrollView) {
        if (toolbar == null || scrollView == null) {
            return null;
        }
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        int svHeight = 0;
        for (int i = 0; i < scrollView.getChildCount(); i++) {
            svHeight += scrollView.getChildAt(i).getHeight();
        }
        int height = svHeight + toolbar.getHeight();
        //
        Bitmap resultBitmap = Bitmap.createBitmap(toolbar.getWidth(), height,
                Bitmap.Config.ARGB_8888);
        Canvas rootCanvas = new Canvas(resultBitmap);
        //
        Bitmap toolbarBitmap = Bitmap.createBitmap(toolbar.getWidth(), toolbar.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas toolbarCanvas = new Canvas(toolbarBitmap);
        toolbarCanvas.drawRGB(255, 255, 255);
        toolbar.draw(toolbarCanvas);
        //
        Bitmap svBitmap = Bitmap.createBitmap(toolbar.getWidth(), svHeight,
                Bitmap.Config.ARGB_8888);
        Canvas svCanvas = new Canvas(svBitmap);
        svCanvas.drawRGB(255, 255, 255);
        scrollView.draw(svCanvas);

        rootCanvas.drawBitmap(toolbarBitmap, 0, 0, paint);
        rootCanvas.drawBitmap(svBitmap, 0, toolbar.getHeight(), paint);
        return resultBitmap;
    }

    private File BitmapToFile(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        String path = HYCrashUtils.getCrashCachePath(this.getApplicationContext());
        File imageFile = new File(path, "HYCrash-" + df.format(model.getTime()) + ".png");
        try {
            FileOutputStream out = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            bitmap.recycle();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return imageFile;
    }

    private void shareImage() {
        File file = BitmapToFile(getBitmapByView(sToolbar, sScrollView));
        if (file == null || !file.exists()) {
            showToast(R.string.simpleImageNotExist);
            return;
        }

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("image/*");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri contentUri = HYCrashFileProvider.getUriForFile(getApplicationContext(), file);
            intent.putExtra(Intent.EXTRA_STREAM, contentUri);
        } else {
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(Intent.createChooser(intent, getString(R.string.simpleShareTo)));
    }

    private void showToast(String text) {
        Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
    }

    private void showToast(@StringRes int textId) {
        Toast.makeText(getApplicationContext(), textId, Toast.LENGTH_SHORT).show();
    }

}
